package base;

import java.util.Arrays;

import org.dom4j.Document;

public class ReflectUtils {
	
	public static void main(String[] args) {
		//1.1
	/*	String temp01 = "typeName";
		String aMeta = getAMeta(temp01);
		System.out.println(temp01+" ==> "+aMeta);*/
		
		//1.
		/*String[] fieldStrings = {"id", "typeName", "fatherId"};
		String[] metasFromFields = getMetasFromFields(fieldStrings );
		System.out.println(Arrays.toString(metasFromFields));*/
		
		//2.
		
		//3.
		System.out.println(getEntityName());
//		System.out.println(getEntityPath());
		
	}
	
	public static String filePath = null;
	public static Document readerXml = null;
	static{
		filePath = "src/main/resources/generate.xml";
		try {
			readerXml = XmlUtil.readerXml(filePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 3.2.2 把字符串的首字母变成大写
	 * @param str 原字符串
	 * @return 字符串的首字母变成大写的字符串
	 */
	public static String getUpper(String str) {
		//1.1 取到str的取出第1个，并变成大写
		String upper = str.substring(0,1).toUpperCase();
		//1.2 取到str的后length-1个
		String ss = str.substring(1, str.length());
		String temp = upper+ss;
		return temp;
	}

	/**
	 * 3.2.1 去掉下划线，拆分成数组，把字符串的首字母变成大写后，合并字符串
	 * @param name	原字符串
	 * @return	
	 */
	public static String upperStandInitialType(String name){
		String[] strArray = name.split("_");
		String newName = "";
		for(String str : strArray){
			newName+= getUpper(str);
		}
		return newName;
	}
	

	/**
	 * 3.2 解析表名，转为实体类名 //6.
	 * @return
	 * @throws Exception
	 */
	public static String getEntityName(){
		String tableName = getTableName();
		
		//1.取出第一个下滑线与最后一个下划线之间内容
		String temp = tableName.substring(tableName.indexOf('_')+1, tableName.lastIndexOf('_'));
		//2.把首字母变成大写，并在后面加上Entity
		String entityName = upperStandInitialType(temp)+"Entity";
		return entityName;
	}
	
	/**
	 * 获取表名
	 * @return
	 */
	public static String getTableName() {
		return readerXml.selectSingleNode("//tableName").getText();
	}

	/**
	 * 3.1 包路径
	 * @return
	 */
	public static String getPackageName() {
		String pack = readerXml.selectSingleNode("//package").getText();
		return pack;
	}
	
	/**
	 * 3. 解析实体类路径
	 * @return
	 */
	public static String getEntityPath(){
		String path = getPackageName()+"."+getEntityName();
		return path;
	}
	
	/**
	 * 2.创建字段和占位符的字符串  (ID, TYPE_NAME, FATHER_ID) ---并设置占位符(?,?,?)
	 * @param metaDatas	字段数组
	 * @return	String数组 (ID, TYPE_NAME, FATHER_ID)，(?,?,?)
	 */
	public static String[] getmetaAndplaceholder(String[] metaDatas) {
		String[] metaAndplaceholder = new String[2];
		String metaString  = "";		//字段
		String placeholder = "(";		//占位符
		for(int index = 0 ; index < metaDatas.length ; index++){
			if(0 == index){
				metaString  += metaDatas[index];
				placeholder += "?";
			}else{
				metaString  += ","+metaDatas[index];
				placeholder += ",?";
			}
		}
		metaString  += "";
		placeholder += ")";
		metaAndplaceholder[0] = metaString;
		metaAndplaceholder[1] = placeholder;
		return metaAndplaceholder;
	}

	/**
	 * 1. 把所有属性字符串，转为 字段格式 字符串  id ==> ID， typeName ==> TYPE_NAME
	 * @param fieldStrings	原String类型数组
	 * @return	字段格式数组
	 */
	public static String[] getMetasFromFields(String[] fieldStrings) {
		String[] newFieldStr = new String[fieldStrings.length];
		for(int index = 0 ; index < fieldStrings.length ; index++){
			newFieldStr[index] = getAMeta(fieldStrings[index]);
		}
		return newFieldStr;
	}

	/**
	 * 1.1 把一个字符串转为字段格式：typeName ==> TYPE_NAME
	 * @param fieldString	一个属性字符串
	 * @return	一个字段字符串
	 */
	private static String getAMeta(String fieldString) {
		StringBuffer sb = new StringBuffer();
		for(int index = 0 ; index < fieldString.length() ; index++){
			char charAt = fieldString.charAt(index);
			
			//找到大写字母，前面添加下划线
			if(charAt>=65 && charAt <= 90){
				sb.append("_"+charAt);
			}else{
				//小写转大写
				sb.append((""+charAt).toUpperCase());
			}
		}
		return sb.toString();
	}

}
